package hashtab;

/**
 * Created by IntelliJ IDEA.
 * User: 杰哥
 * Date: 2020/4/11
 * Time: 16:14
 */
public class HashTabDemo {
    public static void main(String[] args) {

        System.out.println(4 >> 1);

        HashTab hashTab = new HashTab(7);
        hashTab.add(new Emp(1,"一号"));
        hashTab.add(new Emp(2,"二号"));
        hashTab.add(new Emp(3,"三号"));
        hashTab.add(new Emp(8,"八号"));
        hashTab.list();
        hashTab.removeById(1);
        hashTab.list();
        System.out.println(hashTab.findEmpById(2));

    }
}

//创建HashTab 管理多条链表
class HashTab {
    private EmpLinkedList[] empLinkedListsArray;

    private int size;

    HashTab(int size) {
        this.size = size;
        this.empLinkedListsArray = new EmpLinkedList[this.size];
        for (int i = 0; i < size; i++) {
            this.empLinkedListsArray[i] = new EmpLinkedList();
        }
    }

    void add(Emp emp) {
        int i = hashFun(emp.id);
        empLinkedListsArray[i].add(emp);
    }

    void list() {
        for (int i = 0; i < size; i++) {
            empLinkedListsArray[i].list(i+1);
        }
    }

    public Emp findEmpById(int id) {
        int i = hashFun(id);
        Emp emp = empLinkedListsArray[i].findEmpById(id);
        if(emp == null) {
            System.err.printf("没有找到id为%d的雇员\n", id);
        }
        return emp;
    }

    private int hashFun(int id) {
        return id % this.size;
    }

    void removeById(int id) {
        int i = hashFun(id);
        empLinkedListsArray[i].removeByID(id);
    }
}

//表示一个雇员
class Emp {
     int id;
     String name;
     Emp next;

     Emp(int id, String name) {
        this.id = id;
        this.name = name;
    }
}

class EmpLinkedList {
    private Emp head;

     void add(Emp emp) {
        //如果是添加第一个雇员
        if(head == null) {
            head = emp;
            return;
        }
        Emp curEmp = head;
        while (curEmp.next != null) curEmp = curEmp.next;
        curEmp.next = emp;
    }

    //遍历链表的雇员信息
     void list(int no) {
        if(head == null) {
            System.out.printf("第%d链表为空\n", no);
            return;
        }
        System.out.printf("第%d链表信息为：", no);
        Emp curEmp = head;
        while (true) {
            System.out.printf("=> id=%d name=%s\t",curEmp.id, curEmp.name);
            if(curEmp.next == null) {
                System.out.println();
                break;
            }
            curEmp = curEmp.next;
        }
    }

    Emp findEmpById(int id) {
        if(head == null) {
            return null;
        }
        Emp curEmp = head;
        while (true) {
            if(curEmp.id == id) {
                break;
            }
            if(curEmp.next == null) {
                curEmp = null;
                break;
            }
            curEmp = curEmp.next;
        }
        return curEmp;
    }

    void removeByID(int id) {
        Emp curEmp = head;
        if(head != null && curEmp.id == id && head.next == null) {
            head = null;
            return;
        }
        if(head != null && curEmp.id == id) {
            head = head.next;
            return;
        }
        while (true) {
            if(curEmp.next.id == id) {
                curEmp.next = curEmp.next.next;
                break;
            }
            if(curEmp.next.next == null)  {
                break;
            }
        }
    }
}